package com.example.graduation.controller.job;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.graduation.entity.pojo.Address;
import com.example.graduation.entity.pojo.Company;
import com.example.graduation.entity.pojo.Image;
import com.example.graduation.entity.pojo.Job;
import com.example.graduation.mapper.MapDtoMapper;
import com.example.graduation.service.*;
import com.example.graduation.util.ShiroUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Jeffery
 * Job权限的Controller
 */
@RequestMapping("/job")
@Controller
@Slf4j(topic = "JobController")
@Api(tags = "带有job路径的controller")
public class JobController {
    private static final Integer PAGE_SIZE = 10;

    private static final Integer PAGE_NUM=1;

    @Resource
    private JobService jobService;

    @Resource
    private UserService userService;

    @Resource
    private ImageService imageService;

    @Resource
    private MapDtoMapper mapDtoMapper;

    @Resource
    private CompanyService companyService;

    @Resource
    private AddressService addressService;

    /**
     * 分页查询工作
     *
     * @param pageNumber 第几页
     * @return PageInfo
     */
    @GetMapping("/listByPage")
    @SuppressWarnings("all")
    @ApiOperation("分页查询工作")
    @ApiImplicitParam(name = "pageNumber", value = "当前页数", defaultValue = "1")
    public String listByPage(@RequestParam(value = "pageNumber",required = false) Integer pageNumber,Model model) {
        if (pageNumber==null)pageNumber=PAGE_NUM;
        PageHelper.startPage(pageNumber, PAGE_SIZE);
        QueryWrapper<Job> wrapper = new QueryWrapper<>();
        wrapper.eq("job_is_deleted", 0);
        List<Job> list = jobService.list();
        for (Job job : list) {
            job.setAddress(addressService.getById(job.getAddressId()));
            job.setCompany(companyService.getById(job.getCId()));
        }
        PageInfo<Job> jobPageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo",jobPageInfo);
        return "jobList";
    }

    /**
     * 根据工作名称查询工作
     *
     * @param pageNumber 当前页数
     * @param jobName    工cv90jh'8967,h76nu
     *                   作名称
     * @return List
     */
    @GetMapping("/listByJobName")
    @ResponseBody
    @SuppressWarnings("all")
    @ApiOperation("根据工作名称查询工作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "当前页数", defaultValue = "1"),
            @ApiImplicitParam(name = "jobName", value = "工作名称")
    })
    public PageInfo listByJobName(@RequestParam("pageNumber") Integer pageNumber,
                                  @RequestParam("jobName") String jobName) {
        PageHelper.startPage(pageNumber, PAGE_SIZE);
        QueryWrapper<Job> wrapper = new QueryWrapper<>();
        wrapper.eq("job_is_deleted", 0);
        wrapper.like("job_name", jobName);
        List<Job> list = jobService.list();
        return new PageInfo<>(list);
    }

    /**
     * @return 新增结果  可能会有IO异常 或者网络异常
     */
    @PostMapping("/createJob")
    public String createJob(Job job,
                            Model model) {
        log.info("job param:{}",job);
        jobService.save(job);
        return "";
    }

    @PostMapping("/updateJob")
    public String updateJob(Job job,Model model){
        log.info("job param :{}",job);
        if (job.getId()!=null){
            UpdateWrapper<Job> updateWrapper = new UpdateWrapper<>(job);
            updateWrapper.eq("job_id",job.getId());
            jobService.update(updateWrapper);
        }
        return "";
    }

    @PostMapping("/delJob")
    public String delJob(String jobId,Model model){
        log.info("del job:{}",jobId);
        UpdateWrapper<Job> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("job_id",jobId);
        updateWrapper.set("job_has_deleted",1);
        jobService.update(updateWrapper);
        return "";
    }

    /**
     * 后台管理通过id查询，到编辑页面，
     * 选择地址，公司。
     * @return
     */
    @GetMapping("/queryJob")
    public String queryJob(String jobId,Model model){
        log.info("jobId:{}",jobId);
        Job job = jobService.getById(jobId);
        //所有的公司和地址
        List<Company> companies = companyService.list();
        List<Address> addresses = addressService.list();
        model.addAttribute("job",job);
        model.addAttribute("company",companies);
        model.addAttribute("addresses",addresses);
        return "";
    }

    @GetMapping("/jobDetail")
    public String JobDetail(String jobId,Model model){
        String userId = ShiroUtil.getUserId();
        log.info("query jobId:{},userId:{}",jobId,userId);
        List<Image> list=null;
        QueryWrapper<Image> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(userId)){
            queryWrapper.eq("user_id",userId);
            queryWrapper.eq("image_has_deleted",0);
            queryWrapper.eq("image_exc","pdf");
            list = imageService.list();
        }
        Job job = jobService.getById(jobId);
        job.setCompany(companyService.getById(job.getCId()));
        job.setAddress(addressService.getById(job.getAddressId()));
        model.addAttribute("job",job);
        if (list!=null&&list.size()!=0){
            model.addAttribute("images",list);
            return "jobdetail1";
        }
        return "jobdetail";
    }

}
